package com.diary;

import com.diary.database.*;

import java.text.SimpleDateFormat;
import java.util.*;

public class start {
    static Scanner scanner = new Scanner(System.in);

    static String user = "";
    static String password = "";
    public static void main(String[] args) {
        // 主目录
        directory();
    }

    private static void directory() {

        System.out.println("-------------------");
        System.out.println("欢迎使用个人日记系统");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");
        System.out.println("-------------------");

        Scanner scanner = new Scanner(System.in);
        String in = scanner.nextLine();

        if (in.equals("1")){
            int target = u_login();
            if (target == 1){
                directory();
            }
            else {
                logined_directory();
            }

        } else if (in.equals("2")) {
            //注册功能
            u_register();

        } else if (in.equals("3")) {

            System.out.println("系统已退出，期待下次见面！");
        }
        else {
            System.out.println("输入错误，请重新选择！");
            directory();
        }
    }



    private static void logined_directory() {
        System.out.println("-------------------");
        System.out.println("欢迎使用个人日记系统");
        System.out.println("1.新建日记");
        System.out.println("2.查看未完成的日记");
        System.out.println("3.查看已完成的日记");
        System.out.println("4.切换账户");
        System.out.println("5.退出登录");
        System.out.println("-------------------");

        String in = scanner.nextLine();

        if (in.equals("1")){
            u_CreateDiary();
        } else if (in.equals("2")) {
            u_LookDiary_writing(user);
        } else if (in.equals("3")) {
        //这里加入查看已完成的日记
            u_LookDiary_finished(user);
        } else if (in.equals("4")) {
            user = "";
            password = "";
            directory();
        } else if (in.equals("5")) {
            user = "";
            password = "";
            directory();
        }else {
            System.out.println("输入错误，请重新输入");
            logined_directory();
        }
    }

    private static void select_directory() {

    }

    private static void looked_directory() {
        System.out.println("-------------------");
        System.out.println("欢迎使用个人日记系统");
        System.out.println("1.新建日记");
        System.out.println("2.查看未完成的日记");
        System.out.println("3.查看已完成的日记");
        System.out.println("4.切换账户");
        System.out.println("5.退出登录");
        System.out.println("-------------------");



    }





    private static void u_CreateDiary() {
        //新建日记，如果没有对应的用户的数据表，则先进行创建数据表

        //这里通过用户名创建数据表有被注入的可能，后续自己使用的时候将创建用户的权限进行限制即可



        SelectTable selectTable = new SelectTable();
        if (selectTable.select(user)) {
//            System.out.println("已经有相同名称的表");
            //进入写日记
        }
        else {
//            System.out.println("没有相同名称的表");
            //先进行创建表，然后再进行写日记

            CreateTable createTable = new CreateTable();
            createTable.create(user);
//            System.out.println("已经创建了该用户的表");
            System.out.println("欢迎您首次使用此系统。");
        }

        System.out.println("开始愉快的写日记o(*￣▽￣*)ブ");
//        WriteDiary writeDiary = new WriteDiary();

        // 传递用户名，也就是新的表名
//        writeDiary.write(user);
        u_WriteDiary(user);

//        System.out.println("日记创建结束。");

        // 日记创建结束后进入主菜单
        logined_directory();


    }

    private static <Scaner> void u_LookDiary_writing(String user) {
        SelectTable selectTable = new SelectTable();
        if (!selectTable.select(user)) {
            System.out.println("您还没有创建过日记");
            logined_directory();
        }
        else {
            LookDiary lookDiary = new LookDiary();
            List<Object[]> Diary_data = new ArrayList<>();
            Diary_data = lookDiary.select_writing(user);
            if (Diary_data.size() > 0) {

                ArrayList<String> ids = new ArrayList<>();

                for (Object[] row : Diary_data) {
                    ids.add((String) row[0]);
                    String title = row[1].toString();
                    String formattedTitle = title.length() > 30 ? title.substring(0, 27) + "..." : title;
                    System.out.println(String.format("ID: %-3s, Title: %-30s, Date: %s", row[0], formattedTitle, row[2]));
                }

                //然后查看具体的某一篇
                System.out.println("-------------------");
                System.out.println("查找到" + Diary_data.size() + "篇日记");
                System.out.println("输入id号查看日记详细内容，输入0回到主菜单");
                System.out.println("-------------------");


                while (true) {

                    String in = scanner.nextLine();
                    if (in.equals("0")) {
                        logined_directory();
                        break;
                    } else {
                        if (ids.contains(in)) {
                            //输出的记录中有这条id
                            //进入查找id

                            List<Object[]> looklookin = lookDiary.look(in,user);

                            for (Object[] row : looklookin) {
                                System.out.println(row[0]);
                                System.out.println(row[1]);
                                System.out.println(row[2]);
                            }
                            //进入：查看日记菜单
                            String re = Lookdiary_directory_writing(user);
                            //分支：revise 修改日记，remove 伪删除日记，finish将日记标记为完成，0 返回查看日记，有循环显示id的菜单
                            if (re.equals("0")){
                                u_LookDiary_writing(user);
                            } else if (re.equals("revise")) {
                                //在这里加入调用修改日记
                                u_ReviseDiary(user,in);
                                u_LookDiary_writing(user);
                            } else if (re.equals("remove")) {

//                            System.out.println("start的 删除功能被成功调用");
                                ChangeDiaryLife changeDiaryLife = new ChangeDiaryLife();
                                int target = changeDiaryLife.set_delete(user,in);
                                if (target == 1) {
                                    System.out.println("已删除日记");
                                }else {
                                    System.out.println("删除日记失败，请重试。");
                                }
                                u_LookDiary_writing(user);

                            } else if (re.equals("finish")) {
                                ChangeDiaryLife changeDiaryLife = new ChangeDiaryLife();
                                int target = changeDiaryLife.set_finish(user,in);
                                if (target == 1) {
                                    System.out.println("已完成日记");
                                }else {
                                    System.out.println("更改日记失败，请重试。");
                                }

                                //将日记标为完成
                                u_LookDiary_writing(user);
                            }


                            break;
                        }else {
                            System.out.println("输入的值有误，请重新输入。");
                            continue;
                        }
                    }
                }
            }

            //没有查看到数据
            else {
                System.out.println("没有查看到数据");
                logined_directory();
            }
        }
    }


    private static <Scaner> void u_LookDiary_finished(String user) {
        SelectTable selectTable = new SelectTable();
        if (!selectTable.select(user)) {
            System.out.println("您还没有创建过日记");
            logined_directory();
        }
        else {

            LookDiary lookDiary = new LookDiary();
            List<Object[]> Diary_data = new ArrayList<>();
            Diary_data = lookDiary.select_finished(user);
            if (Diary_data.size() > 0) {

                ArrayList<String> ids = new ArrayList<>();

                for (Object[] row : Diary_data) {
                    ids.add((String) row[0]);
                    String title = row[1].toString();
                    String formattedTitle = title.length() > 30 ? title.substring(0, 27) + "..." : title;
                    System.out.println(String.format("ID: %-3s, Title: %-30s, Date: %s", row[0], formattedTitle, row[2]));
                }

                //然后查看具体的某一篇
                System.out.println("-------------------");
                System.out.println("查找到" + Diary_data.size() + "篇日记");
                System.out.println("输入id号查看日记详细内容，输入0回到主菜单");
                System.out.println("-------------------");


                while (true) {

                    String in = scanner.nextLine();
                    if (in.equals("0")) {
                        logined_directory();
                        break;
                    } else {
                        if (ids.contains(in)) {
                            //输出的记录中有这条id
                            //进入查找id

                            List<Object[]> looklookin = lookDiary.look(in,user);

                            for (Object[] row : looklookin) {
                                System.out.println(row[0]);
                                System.out.println(row[1]);
                                System.out.println(row[2]);
                            }
                            //进入：查看日记菜单
                            String re = Lookdiary_directory_finish(user);
                            //分支：revise 修改日记，remove 伪删除日记，finish将日记标记为完成，0 返回查看日记，有循环显示id的菜单
                            if (re.equals("0")){
                                u_LookDiary_writing(user);
                            } else if (re.equals("revise")) {
                                //在这里加入调用修改日记
                                u_ReviseDiary(user,in);
                                u_LookDiary_writing(user);
                            } else if (re.equals("remove")) {

//                            System.out.println("start的 删除功能被成功调用");
                                ChangeDiaryLife changeDiaryLife = new ChangeDiaryLife();
                                int target = changeDiaryLife.set_delete(user,in);
                                if (target == 1) {
                                    System.out.println("已删除日记");
                                }else {
                                    System.out.println("删除日记失败，请重试。");
                                }
                                u_LookDiary_writing(user);

                            } else if (re.equals("writing")) {
                                ChangeDiaryLife changeDiaryLife = new ChangeDiaryLife();
                                int target = changeDiaryLife.set_writing(user,in);
                                if (target == 1) {
                                    System.out.println("已将日记标为未完成");
                                }else {
                                    System.out.println("更改日记失败，请重试。");
                                }

                                //将日记标为完成
                                u_LookDiary_writing(user);
                            }


                            break;
                        }else {
                            System.out.println("输入的值有误，请重新输入。");
                            continue;
                        }
                    }
                }
            }

            //没有查看到数据
            else {
                System.out.println("没有查看到数据");
                logined_directory();
            }
        }
    }


    private static String Lookdiary_directory_writing(String user) {
        String re = "0";
        System.out.println("输入revise 修改日记，finish 将日记标为完成，remove 删除日记，0 返回");
        while (true) {
            re = scanner.nextLine();
            if (re.equals("0") || re.equals("revise") || re.equals("remove") || re.equals("finish"))
                return re;
            else
                System.out.println("请重新输入");
        }
    }

    private static String Lookdiary_directory_finish(String user) {
        String re = "0";
        System.out.println("输入revise 修改日记，writing 将日记标为未完成，remove 删除日记，0 返回");
        while (true) {
            re = scanner.nextLine();
            if (re.equals("0") || re.equals("revise") || re.equals("remove") || re.equals("writing"))
                return re;
            else
                System.out.println("请重新输入");
        }
    }

    private static void u_WriteDiary(String user) {

        // 后续将数据库操作移出到database，作为添加记录类；将本类作为调用方法

        Scanner in = new Scanner(System.in);

        System.out.println("请输入今天的日记标题");
        String diary_title = in.nextLine();

        System.out.println("请输入一段结束码，检测到结束码时结束日记");
        String ending = in.nextLine();

        System.out.println("结束码是：" + ending);

        System.out.println("请开始写正文，输入结束码意味着结束日记。");

        StringBuilder stringBuilder = new StringBuilder();
        String line;

        // 检测到安全词则退出
        while (!(line = in.nextLine()).equals(ending)) {
            stringBuilder.append(line).append("\n");
        }
        String body = stringBuilder.toString();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datatime = simpleDateFormat.format(new Date());

        System.out.println(datatime);

        WriteDiary writeDiary = new WriteDiary();
        writeDiary.write(user,diary_title,body,datatime,user);

    }

    private static void u_ReviseDiary(String user,String id) {
        Scanner in = new Scanner(System.in);

        System.out.println("请输入今天的日记标题");
        String diary_title = in.nextLine();

        System.out.println("请输入一段结束码，检测到结束码时结束日记");
        String ending = in.nextLine();

        System.out.println("结束码是：" + ending);

        System.out.println("请开始写正文，输入结束码意味着结束日记。");

        StringBuilder stringBuilder = new StringBuilder();
        String line;

        // 检测到安全词则退出
        while (!(line = in.nextLine()).equals(ending)) {
            stringBuilder.append(line).append("\n");
        }
        String body = stringBuilder.toString();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datatime = simpleDateFormat.format(new Date());

        System.out.println(datatime);

        ReviseDiary reviseDiary = new ReviseDiary();
        ReviseDiary.re_write(user,id,diary_title,body,datatime,user);


    }



    private static int u_login() {
        System.out.println("请输入账号：");
        user = scanner.nextLine();
        System.out.println("请输入密码：");
        password = scanner.nextLine();

        if (login.in(user,password)){
            System.out.println("登录成功！");
            return 0;
        }
        else {
            System.out.println("登陆失败！");
            return 1;
        }
    }
    private static void u_register() {

        System.out.println("用户名：");
        user = scanner.nextLine();
        System.out.println("密码：");
        password = scanner.nextLine();
        System.out.println("再次输入密码以确认：");
        String test = scanner.nextLine();

        //进行用户名检测
        String regex = "^[\\u4e00-\\u9fa5a-zA-Z0-9_]{4,20}$";
        if (!user.matches(regex)) {
            System.out.println("用户名不合规，请重新输入合适的用户名。");
            System.out.println("用户名可以包含中文、英文、数字和下划线，长度在4-20个字符之间");
            u_register();
        }


        if (password.equals(test)) {
            //在数据表中查找是否有相同的用户名
            Find find = new Find();
            if (find.FindUser(user)) {
                System.out.println("已有相同名称的用户，请重新输入用户名密码");
                u_register();
            }
            else {
                if (CreateUser.add(user,password)) {
                    System.out.println("注册成功,请重新登陆");
                    directory();
                }
                else {
                    System.out.println("注册失败，请重新尝试");
                    u_register();
                }
            }
        }
        else {
            System.out.println("两次输入的密码不同，请重试");
            u_register();
        }
    }
}






